package com.xiu.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xiu.pojo.ExamGrade;
import com.xiu.pojo.ExamSubject;
import com.xiu.pojo.ExamTi;
import com.xiu.pojo.ExamUser;
import com.xiu.service.ExamUserService;
import com.xiu.service.StudentService;

@Controller
@RequestMapping(value="/exam")
public class StudentController {

	@Autowired
	StudentService studentService;
	
	@Autowired
	ExamUserService examUserService;
	
	
	//学生查看科目
	@RequestMapping(value="/stuSubjectList",method=RequestMethod.GET)
	public String showSubject(Model model,ExamSubject examSubject,HttpServletRequest request,HttpSession session){
		Integer examStuId = (Integer) request.getSession().getAttribute("userId");
		List<ExamSubject> stuSubjectList = studentService.stuFindSubjects(examStuId);
		if(stuSubjectList.size()>0){
			model.addAttribute("stuSubjectList", stuSubjectList);
			model.addAttribute("examSubject", examSubject);
		}
		return "exam/stuSubjects";
	}
	
	//学生查看成绩
	@RequestMapping(value="/goGrade/{examSubjectId}",method=RequestMethod.GET)
	public String showGrade(@PathVariable int examSubjectId,Model model,ExamGrade examGrade){
		List<ExamGrade> stuGradeList = studentService.stuFindGrade(examSubjectId);
		if(stuGradeList.size()>0){
			model.addAttribute("stuGradeList", stuGradeList);
			model.addAttribute("examGrade", examGrade);
		}
		return "exam/stuGrade";
	}
	
	//学生查看他的所有成绩
	@RequestMapping(value="/stuGrade",method=RequestMethod.GET)
	public String showGrade(Model model,ExamGrade examGrade,HttpServletRequest request,HttpSession session){
		Integer examStuId = (Integer) request.getSession().getAttribute("userId");
		List<ExamGrade> stuGradeList = studentService.stuShowGrade(examStuId);
		if(stuGradeList.size()>0){
			model.addAttribute("stuGradeList", stuGradeList);
			model.addAttribute("examGrade", examGrade);
		}
		return "exam/stuGrade";
	}
	
	//学生去准备界面
	@RequestMapping(value="/goExam/{examSubjectId}",method=RequestMethod.GET)
	public String goExam(@PathVariable int examSubjectId,Model model,HttpServletResponse response,HttpServletRequest request, HttpSession session){
		ExamSubject examSubject = studentService.stuExamTime(examSubjectId);
		int examTime = examSubject.getExamSubjectTotaltime();
		//拿到科目考试总时长
		request.getSession().setAttribute("examTime", examTime);
		request.getSession().setAttribute("examSubjectId", examSubjectId);
		int count = 0;
		session.setAttribute("count", count);
		return "exam/examReady";
	}
	
	//去考试
	@RequestMapping(value="/stuExam",method=RequestMethod.GET)
	public String goExam(Model model,ExamTi examTi,HttpServletRequest request, HttpSession session){
		Integer examSubjectId =  (Integer) session.getAttribute("examSubjectId");
		List<ExamTi> stuTiList = studentService.stuExam(examSubjectId);
		System.out.println("========="+stuTiList.toString());
		model.addAttribute("stuTiList", stuTiList);
		model.addAttribute("examTi", examTi);
		
		ExamSubject subject = studentService.stuExamTime(examSubjectId);
		Date startdate=null;
		if(request.getSession().getAttribute("startdate")==null)
			startdate = new Date();
		else
			startdate = (Date) request.getSession().getAttribute("startdate");
		int totaltime = subject.getExamSubjectTotaltime()*60*1000;
		request.getSession().setAttribute("totaltime", totaltime);
		int sparetime = 0;
		Date nowtime = new Date();
		int dotime = (int) (nowtime.getTime()-startdate.getTime());
		if(dotime<totaltime){
			sparetime=totaltime-dotime;
		}
		
		request.getSession().setAttribute("nowsubject", subject);
		request.getSession().setAttribute("startdate", startdate);
		model.addAttribute("sparetime",sparetime);
		
		return "exam/stuExam";
	}
	
	@RequestMapping(value="/time",method=RequestMethod.POST)
	public void time(HttpServletRequest request,HttpServletResponse response) throws JSONException{
		System.out.println("111");
		JSONObject json=new JSONObject();
		Date startdate = (Date) request.getSession().getAttribute("startdate");
		ExamSubject subject = (ExamSubject) request.getSession().getAttribute("nowsubject");
		int totaltime = subject.getExamSubjectTotaltime()*60*1000;
		request.getSession().setAttribute("totaltime", totaltime);
		int sparetime = 0;
		Date nowtime = new Date();
		int dotime = (int) (nowtime.getTime()-startdate.getTime());
		if(dotime<totaltime){
			sparetime=totaltime-dotime;
		}
		json.put("spareTime", sparetime);
		json.put("result","success");
		try {
			response.getWriter().write(json.toString());
		} catch (IOException e) {
			return;
		}
	}

@RequestMapping(value="/choose/{idid}",method=RequestMethod.GET)
	
	public void saveChoose(@PathVariable int idid,Model model,HttpServletRequest request,String array,HttpSession session,HttpServletResponse response) throws IOException{
	
		
		
	     String array1=request.getParameter("array");
	     String array2=array1.substring(1, array1.length()-1);
	     String array3 = array2.substring(1, array2.length()-1);
	     System.out.println(array3);
	     
	     Integer examSubjectId =  (Integer) session.getAttribute("examSubjectId");
	     ExamSubject examSubject = studentService.findDone(examSubjectId);
	     Integer userId = (Integer) session.getAttribute("userId");
	     ExamTi examTi = studentService.findKey(examSubjectId);
	     String examKey = examTi.getExamTiKey();
	     System.out.println("========"+examKey);
	     int point = examTi.getExamTiPoint();
	     System.out.println("====sssssssss===="+point);
	     
	     ExamGrade examGrade = new ExamGrade();
	     int sum = 0;
	     if(examKey.equals(array3)==true){
	    	 System.out.println(66666);
	    	 sum = sum + point ;
	    	 
	    	 System.out.println(sum);
	     }
	     examGrade.setExamGradePoint(sum);
	     examGrade.setExamSubjectId(examSubjectId);
	     examGrade.setExamStuId(userId);
	     examGrade.setExamGradeFlag(1);
	     int countGrade = studentService.addGrade(examGrade);
	     System.out.println(examGrade.toString());
	     
	     examSubject.setExamSubjectDone(1);
	     int countSubject = studentService.editSubject(examSubject);
	     
	     JSONObject json=new JSONObject();

	    	 try {
	    		if(countGrade ==1&&countSubject==1){
	    			 json.put("result", "success66");
				}else{
			    	 json.put("result", "error");
			     }
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	 response.getWriter().write(json.toString());
	     }
	     
	     //String[] chooses=array2.split(",");
	
	     

	     /* List<ExamTi> list=examTiMapper.selectAllExamTi();
	       int sum=0;
	      for(int i=0;i<chooses.length;i++){
	    	    String key=list.get(i).getExamTiKey();
	    	    Integer point=list.get(i).getExamTiPoint();
	    	    if(chooses[i].substring(1,2).equals(key)){
	    	    	sum=sum+point.intValue();
	    	    }
	      }*/
	      //System.out.println("总分是"+sum);
	      

			/*String number=(String)request.getSession().getAttribute("examStuNumber");
			ExamStu examStu=examStuMapper.selectByExamStuNumber(number);
			System.out.println(examStu.getExamStuId()+"-------------");
			//一个学生对应多个科目，并且查询多个科目的done，判断该考生是否已经考过该科目，
			//如果你部通过中间表去去查询过，把学生考试done写在student中，只能说明该考试是否考过试题而已不能够说明他考过的多个科目
			List<ExamStudentSubject> examStudentSubject=examStudentSubjectMapper.selectByExamStuId(examStu.getExamStuId());
		  
			//通过Id和subjectId查找科目查找出成绩单
			ExamGrade examGrade=new ExamGrade();
			
			examGrade.setExamStuId(examStu.getExamStuId());
			examGrade.setExamSubjectId(idid);
			
			ExamGrade examGrade1=examGradeMapper.selectByStuIdAndsubjectId(examGrade); 
            double sumDouble;
            //遇到
           sumDouble=Double.valueOf(sum+"");
            System.out.println("科目总分"+sumDouble); 
		     examGrade.setExamGradeSum(sumDouble);     
             examGrade.setExamGradeId(examGrade1.getExamGradeId()); 
             System.out.println("---------");
		    //更新数据库
		    examGradeMapper.updateByPrimaryKeySelective(examGrade);*/
	}
	


